{include file="public/_header" /}
<style>
    #mainForm {
        max-width: 700px;
        margin: 30px auto;
    }

    #mainForm .layui-form-item {
        margin-bottom: 25px;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">添加菜单</div>
        <div class="layui-card-body">
            <form class="layui-form" id="mainForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单:</label>
                    <div class="layui-input-block">
                        <select name="pid" id="pid">
                            <option value="0">--请选择--</option>
                            {volist name="menus" id="menu"}
                            <option value="{$menu['id']}">{$menu.lefthtml}{$menu.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">菜单标题:</label>
                    <div class="layui-input-block">
                        <input name="title" id="title" placeholder="请输入" class="layui-input"
                               lay-verType="tips"/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">URL:</label>
                    <div class="layui-input-block">
                        <input name="url" id="url" placeholder="请输入" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">排序:</label>
                    <div class="layui-input-block">
                        <input name="sort" id="sort" placeholder="请输入" class="layui-input" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">是否是菜单:</label>
                    <div class="layui-input-block">
                        <select name="is_menu" id="isMenu">
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">CSS:</label>
                    <div class="layui-input-block">
                        <input name="css" id="css" placeholder="请输入" class="layui-input" lay-verType="tips" />
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">描述:</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="描述" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="form-group-bottom text-right">
                    <button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>
                    <button class="layui-btn" lay-filter="add" lay-submit>&emsp;提交&emsp;</button>
                </div>
            </form>
        </div>
    </div>
</div>
{include file="public/_footer"}
<script>
    layui.use(['form', 'element'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        form.on('submit(add)', function (data) {
            var arr = data.field;
            if (arr.title.trim() === '') {
                layer.msg('菜单标题不能为空');
                return false;
            }

            if (arr.url.trim() === '') {
                layer.msg('菜单URL不能为空!');
                return false;
            }
            if (arr.pid > 0) {
                arr.type = 2;
            }
            // 提交数据
            $.post('{:url("add")}', arr, function (res) {
                if(res.code === 0) {
                    layer.msg(res.msg);
                    setTimeout(function () {
                        window.location.href = "{:url('index')}";//'index.html';
                    },500);
                }else {
                    layer.msg(res.msg);
                }
            },'JSON');
            return false;
        });


    })

</script>
</body>
</html>